package com.login;

import njust.edu.dao.DishDao;
import njust.edu.dao.TradeDao;
import njust.edu.dao.UserDao;
import njust.edu.dao.impl.DishDaoImpl;
import njust.edu.dao.impl.TradeDaoImpl;
import njust.edu.dao.impl.UserDaoImpl;
import njust.edu.dbutils.SimpleUtils;
import njust.edu.entity.Dish;
import njust.edu.entity.Trade;
import njust.edu.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;

import java.sql.*;


@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type","text/html;charset=utf-8");

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        
        try {
            //Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/cater_system?user=root&password=123456&useUnicod \n" +
                    //"e=true&characterEncoding=GBK");
            Connection conn= SimpleUtils.getConnection();
            Statement stmt = conn.createStatement();
            UserDao userDao = new UserDaoImpl();
            User t = userDao.findById(513l);
            out.println("<h1>" + t.getName() + "</h1>");
            String sql = "select * from user";
            ResultSet rs = stmt.executeQuery(sql);
            String a = "null";
            String b = "null";
            while(rs.next()) {
                a = rs.getString(1);
                b = rs.getString(2);
                out.println("<h1>" + b + "</h1>");
                //System.out.println("yes");
            }
            SimpleUtils.release(conn,stmt,rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
        out.println("</body></html>");

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}



